﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Collections.Generic;
using System.IO;

public partial class Manage_Service_Service_Index : System.Web.UI.Page
{
    public string m = PageRequest.GetString("m");
    public string p = PageRequest.GetString("p");

    KeRui.BLL.CmsServer bllServer = new KeRui.BLL.CmsServer();
    KeRui.BLL.CmsSite bllSite = new KeRui.BLL.CmsSite();
    KeRui.BLL.CmsNode bllNode = new KeRui.BLL.CmsNode();
    KeRui.BLL.CmsContent bllContent = new KeRui.BLL.CmsContent();
    KeRui.BLL.Common bllCommon = new KeRui.BLL.Common();

    protected void Page_Load(object sender, EventArgs e)
    {
        KeRui.Model.CmsServer modelServer = bllServer.GetModel(PageRequest.GetServerIP());
        if (modelServer == null)
        {
            Response.Write("-996");
            Response.End();
        }

        if (Utils.MD5(p+modelServer.ServerKey).ToLower() != m.ToLower())
        {
            Response.Write("-997");
            Response.End(); 
        }

        switch (PageRequest.GetString("act").ToLower().Trim())
        {
            case "checkserver":
                {
                    Response.Write("1");
                    Response.End();
                    break;
                }
            case "getsite":
                {
                    List<KeRui.Model.CmsSite> listSite= bllSite.GetList(modelServer.ServerID, 1);
                    foreach (KeRui.Model.CmsSite model in listSite)
                    {
                        Response.Write(model.SiteDomain+"|");
                    }
                    Response.End();
                    break;
                }
            case "contentdelete":
                {
                    ContentDelete();
                    break;
                }
            case "checkcatch":
                {
                    DataSet ds = bllCommon.GetList("cmscatch", 1, 1, "ServerID=" + modelServer.ServerID + " AND isUsing =1", "LastTime DESC");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        Response.Write("1900-1-1 0:0:0");
                        Response.End();
                    }
                    else
                    {
                        Response.Write(ds.Tables[0].Rows[0]["LastTime"].ToString());
                        Response.End();
                    }
                    break;
                }
            case "getcontenttitle":
                {
                    GetContentTitle();
                    break;
                }
            case "getcontenturl":
                {
                    GetContentUrl();
                    break;
                }
            default:
                break;
        }

        Response.Write("-995");//请求异常
        Response.End();
    }

    void GetContentUrl()
    {
        string Title = p;
        if (Title =="")
        {
            Response.Write("-1");
            Response.End();
        }

        Title = Utils.UrlDecode(Title);
        //搜索文章
        KeRui.Model.CmsNode modelNode = null;
        KeRui.Model.CmsSite modelSite = null;
        List<KeRui.Model.CmsContent> listContent = bllContent.GetList("IsDelete=0 and title='"+Utils.SqlStringFormat(Title)+"'", -1, -1);
        foreach (KeRui.Model.CmsContent modelContent in listContent)
        {
            modelNode = bllNode.GetModelByCache(modelContent.NodeID);
            if (modelNode != null)
            {
                modelSite = bllSite.GetModelByCache(modelNode.SiteID);
                if (modelSite != null)
                {
                    Response.Write(modelSite.SiteUrl + "/" + modelNode.NodePath + "/" + modelContent.ContentID.ToString() + ".shtml|");
                  }
            }
        }
        Response.End();
    }

    void GetContentTitle()
    {
        #region
        int ContentID = Utils.StrToInt(p, 0);
        if (ContentID <= 0)
        {
            Response.Write("-1");
            Response.End();
        }
        KeRui.Model.CmsContent modelContent = bllContent.GetModel(ContentID);
        if (modelContent == null)
        {
            Response.Write("-2");
            Response.End();
        }

        Response.Write(Utils.UrlEncode(modelContent.Title.Trim()));
        Response.End();

      
        #endregion
    }

    void ContentDelete()
    {
        #region
        int ContentID = Utils.StrToInt(p, 0);
        if (ContentID <= 0)
        {
            Response.Write("-1");
            Response.End();
        }
        KeRui.Model.CmsContent modelContent = bllContent.GetModel(ContentID);
        if (modelContent == null)
        {
            Response.Write("-2");
            Response.End();
        }
        KeRui.Model.CmsNode modelNode = bllNode.GetModel(modelContent.NodeID);
        if (modelNode == null)
        {
            Response.Write("-3");
            Response.End();
        }
        #endregion

        string dir = modelNode.NodeDir;

        string file = dir + "\\" + ContentID.ToString() + ".shtml";
        try
        {
            if (File.Exists(file))
            {
                File.Delete(file);
            }
        }
        catch {
            Response.Write("-4");
            Response.End();
        }

        try
        {
            bllContent.FixPreNextID(ContentID, "delete");
            bllContent.Delete(ContentID);
        }
        catch
        {
            Response.Write("-5");
            Response.End();
        }

        Response.Write("1");
        Response.End();
    }
}
